package com.msunsoft.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.msunsoft.common.BaseController;
import com.msunsoft.common.Constants;
import com.msunsoft.common.ReponseBodyVo;
import com.msunsoft.system.mybatis.model.SysMenu;
import com.msunsoft.system.mybatis.model.SysUser;
import com.msunsoft.system.service.impl.MenuService;
import com.msunsoft.system.vo.MenuTreeVo;

@Controller
@RequestMapping(value = "/menu")
public class MenuAdminController extends BaseController {

    @Resource
    private MenuService menuService;

    @RequestMapping("")
    @RequiresPermissions("sys:menu:view")
    public String index() {
        return "admin/menu/menu";
    }

    @ResponseBody
    @RequestMapping("/get-tree")
    @RequiresAuthentication
    public List<MenuTreeVo> getTree() {
        return this.menuService.selectTreeByType(null);
    }

    /**
     * 查询当前用户有权管理的可见菜单 用户后台首页左侧的树
     * @return 可见菜单
     */
    @ResponseBody
    @RequestMapping("/get-tree-by-user")
    @RequiresAuthentication
    public List<MenuTreeVo> selectTreeByUser(HttpServletRequest req, HttpSession session) {
        SysUser user = (SysUser)session.getAttribute(Constants.SESSION_USER);
        Integer id =user.getId();
        return this.menuService.selectTreeByUser(id);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("sys:menu:insert")
    public ReponseBodyVo insert(@RequestBody SysMenu menu) {
        this.menuService.insert(menu);
        return new ReponseBodyVo("成功", "插入菜单信息成功.");
    }

    @RequestMapping(value = "/edit-by-id", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("sys:menu:edit")
    public ReponseBodyVo update(@RequestBody SysMenu menu) {
        this.menuService.updateById(menu);
        return new ReponseBodyVo("成功", "修改菜单信息成功.");
    }

    @RequestMapping(value = "/del-by-id")
    @ResponseBody
    @RequiresPermissions("sys:menu:del")
    public ReponseBodyVo deleteById(Integer id) {
        this.menuService.deleteById4Tree(id);
        return new ReponseBodyVo("成功", "删除菜单信息成功.");
    }
}
